package com.blindBox.back.controller.getDataController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blindBox.back.annotation.JwtToken;
import com.blindBox.back.commonResult.MsgBackToFront;
import com.blindBox.back.domain.User;
import com.blindBox.back.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

//获取后台需要的用户数据
@RestController
@RequestMapping("/paper")
public class BackDataUserController
{
    @Autowired
    private UserService userService;
    //获取所有用户
    @JwtToken
    @PostMapping("/get_all_users")
    public MsgBackToFront getAllUsers()
    {
        List<User> userList = userService.list();
        return userList!=null?new MsgBackToFront(userList,MsgBackToFront.HANDLE_SUCCESS):
                new MsgBackToFront(null,MsgBackToFront.NO_USER);
    }

    //模糊搜索的用户
    @JwtToken
    @PostMapping("/get_target_user")
    public MsgBackToFront getTargetUser(@RequestParam("u_name")String u_name)
    {
        List<Map<String, Object>> maps = userService.listMaps(new QueryWrapper<User>().like("u_name", u_name));
        return new MsgBackToFront(maps,MsgBackToFront.HANDLE_SUCCESS);
    }

    //获取男性用户
    @JwtToken
    @PostMapping("/get_man_users")
    public MsgBackToFront getManUsers(HttpServletRequest  request)
    {
        List<User> userList = userService.list(new QueryWrapper<User>().eq("sex", 1));
        return userList!=null?new MsgBackToFront(userList,MsgBackToFront.HANDLE_SUCCESS)
                :new MsgBackToFront(null,MsgBackToFront.NO_USER);
    }

    //获取女性用户
    @JwtToken
    @PostMapping("/get_woman_users")
    public MsgBackToFront getWomanUsers(HttpServletRequest request)
    {
        List<User> userList = userService.list(new QueryWrapper<User>().eq("sex", 0));
        return userList!=null?new MsgBackToFront(userList,MsgBackToFront.HANDLE_SUCCESS)
                :new MsgBackToFront(null,MsgBackToFront.NO_USER);
    }

    //获取男女用户比例
    @JwtToken
     @PostMapping("/get_rate_ret")
     public MsgBackToFront getRate(HttpServletRequest  request)
   {
       //查询男性用户的数量
       double manNums = userService.getManNums();
       //查询女性用户的数量
       double womanNums = userService.getWomanNums();
       double radio = (manNums / (manNums + womanNums)) * 100;
       return  new MsgBackToFront(radio,MsgBackToFront.HANDLE_SUCCESS);
   }

    //获取总用户数量
    @JwtToken
    @PostMapping("/get_user_num")
    public MsgBackToFront getUserNums(HttpServletRequest request)
    {
        Integer allUsersNum = userService.getAllUsersNum();
        return new MsgBackToFront(allUsersNum,MsgBackToFront.HANDLE_SUCCESS);
    }


    //通过纸条id获取到拥有者的信息
    @PostMapping("get_paper_user_info")
    @JwtToken
    public MsgBackToFront get_paper_user_info(@RequestParam("p_u_id")Integer p_u_id)
    {
        User paper_user_info = userService.getById(p_u_id);
        if(paper_user_info==null)return new MsgBackToFront(null,MsgBackToFront.DATABASE_ERROR);
        return new MsgBackToFront(paper_user_info,MsgBackToFront.HANDLE_SUCCESS);
    }

    //获取抽中纸条人的性别
    @PostMapping("get_chose_user_sex")
    @JwtToken
    public MsgBackToFront get_chose_user_sex(@RequestParam("chose_name")String chose_name)
    {
        User get_chose_user_sex = userService.getOne(new QueryWrapper<User>().eq("uname",chose_name));
        if(get_chose_user_sex==null)return new MsgBackToFront(null,MsgBackToFront.DATABASE_ERROR);
        return new MsgBackToFront(get_chose_user_sex,MsgBackToFront.HANDLE_SUCCESS);
    }

    //封号
    @PostMapping("/banned_user_by_id")
    @JwtToken
    public MsgBackToFront banned_user_by_id(@RequestParam("u_id")Integer u_id)
    {
        return userService.banUserById(u_id);
     }

     //解封
    @PostMapping("/remove_banned_user_by_id")
    @JwtToken
    public MsgBackToFront remove_banned_user_by_id(@RequestParam("u_id")Integer u_id)
    {
      return userService.removceBanUserById(u_id);
    }

    //删除用户
    @PostMapping("/del_user_by_id")
    @JwtToken
    public MsgBackToFront del_user_by_id(@RequestParam("u_id")Integer u_id)
    {
     return userService.delUserById(u_id);
    }
}
